package com.hd.trans.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.hd.trans.db.DataBaseMgr;
import com.hd.trans.db.bean.HuDunLanguage;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hms.mlsdk.model.download.MLLocalModelManager;
import com.huawei.hms.mlsdk.model.download.MLRemoteModel;
import com.huawei.hms.mlsdk.translate.local.MLLocalTranslatorModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class OfflineConstant {

    //离线包下载状态
    private final static int NOT_DOWNLOADED = 1;//待下载
    private final static int DOWNLOADING = 2;//正在下载
    private final static int DOWNLOADED = 3;//已下载

    //网络状态
    public final static int NETWORL_NONE = 1;
    public final static int NETWORK_WIFI = 2;
    public final static int NETWORK_MOBILE = 3;

    //英文版语言名称
    public static final List<String> LANGUAGE_LIST_EN = new ArrayList<>(Arrays.asList(
            "Chinese (Simplified)", "Chinese (Traditional)", "English", "Spanish", "German", "Russian", "French", "Italian",
            "Portuguese", "Thai", "Arabic", "Turkish", "Japanese", "Danish", "Polish",
            "Finnish", "Korean", "Swedish", "Vietnamese", "Malaysian", "Norwegian",
            "Indonesian", "Czech", "Hebrew", " Greek", "Hindi", "Filipino", "Serbian",
            "Romanian","Tamil","Hungarian","Netherlands","Persian","Slovak","Estonian",
            "Latvian","Khmer"));

    //中文版语言名称
    public static final List<String> LANGUAGE_LIST_ZH = new ArrayList<>(Arrays.asList(
            "中文（简体）", "中文（繁体）", "英语", "西班牙语", "德语", "俄语", "法语", "意大利", "葡萄牙", "泰语",
            "阿拉伯", "土耳其", "日语", "丹麦语", "波兰语", "芬兰语", "韩语", "瑞典语", "越南语",
            "马来西亚语", "挪威语", "印尼语", "捷克语", "希伯来语", "希腊语", "印地语",
            "菲律宾语", "塞尔维亚语", "罗马尼亚语","泰米尔语","匈牙利语","荷兰语","波斯语",
            "斯洛伐克语","爱沙尼亚语","拉脱维亚语","高棉语"));

    //(支持离线翻译的语种的code)
    public static final List<String> DOWNLOAD_CODE_LIST = new ArrayList<>(Arrays.asList(
            "en","zh", "es", "de", "ru", "fr", "it", "pt", "th", "ar", "tr",
            "ja", "da", "pl", "fi", "ko", "sv", "vi", "ms", "no", "id",
            "cs", "he", "el", "hi", "tl", "sr", "ro","ta","hu","nl","fa",
            "sk","et","lv","km"));

    public static final List<String> DOWNLOAD_CODE_LIST_NEW = new ArrayList<>(Arrays.asList(
            "en", "zh", "ko", "de", "ja", "hi", "pt", "lv",
            "hr", "hu", "uk", "id", "ur", "mk", "ms",
            "el", "mt", "is", "it", "my", "es", "et", "ar", "vi",
            "fa", "ro", "nl", "no", "ru", "fi", "bg", "fr", "bn", "bs",
            "ka", "sk", "sl", "ca", "sr", "km", "sv", "ta",
            "cs", "th", "tl", "pl", "da", "he", "tr"
    ));

    public static final List<HuDunLanguage> DOWNLOAD_LANG_LIST = new ArrayList<>();

    static {
        for(String code : DOWNLOAD_CODE_LIST_NEW){
            HuDunLanguage huDunlanguage = DataBaseMgr.getInstance().getLanguageByTransCode(code);
            DOWNLOAD_LANG_LIST.add(huDunlanguage);
        }
    }

    //兆
    public final static long M = 1024 * 1024;

    public static int checkNetwork(Context context) {

        ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            NetworkInfo networkInfo = connectMgr.getActiveNetworkInfo();
            if (networkInfo == null || !networkInfo.isAvailable() || !networkInfo.isConnected()) {
                return NETWORL_NONE;
            }

            int networkType = networkInfo.getType();
            if (networkType == ConnectivityManager.TYPE_WIFI) {
                return NETWORK_WIFI;
            }

            if (networkType == ConnectivityManager.TYPE_MOBILE) {
                return NETWORK_MOBILE;
            }
        } catch (Exception e) {
            return NETWORL_NONE;
        }

        // Others: no network by default
        return NETWORL_NONE;
    }

    public static MLLocalModelManager manager;
    public static ArrayList<String> downloadedModelList = new ArrayList<>();

    public static void updateModel() {
        if(manager == null){
            manager = MLLocalModelManager.getInstance();
        }
        manager.getModels(MLLocalTranslatorModel.class).addOnSuccessListener(new OnSuccessListener<Set<MLLocalTranslatorModel>>() {
            @Override
            public void onSuccess(Set<MLLocalTranslatorModel> mlLocalTranslatorModels) {

                downloadedModelList.clear();
                downloadedModelList.add("en");//英文不需要下载，已经存在sdk里面
                for (MLRemoteModel m : mlLocalTranslatorModels) {
                    String modelName = m.getModelName().substring(m.getModelName().indexOf("_") + 1);
                    if (!downloadedModelList.contains(modelName)) {
                        downloadedModelList.add(modelName);
                    }
                }
            }
        });
    }
}
